package com.manner.groupon.model.enums;

import cn.hutool.core.collection.CollUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: Manner
 * @Description: 订单状态枚举
 * @DateTime: 2024/10/12 21:44
 */
public enum OrderStatus {
    /**
     * 待支付
     */
    TO_BE_PAID(0, "待支付"),

    /**
     * 已取消
     */
    CANCELED(1, "已取消"),

    /**
     * 已支付
     */
    HAVE_PAID(2, "已支付"),

    /**
     * 已退款
     */
    REFUNDED(3, "已退款"),

    /**
     * 已使用
     */
    HAVE_BEEN_USED(4, "已使用"),

    /**
     * 已过期
     */
    HAVE_EXPIRED(5, "已过期"),

    /**
     * 退款中
     */
    REFUND_LOADING(6, "退款中"),

    /**
     * 支付未成功
     */
    PAY_FAIL(7, "支付未成功"),

    REFUND_FAIL(8, "退款异常");

    private final Integer code;
    private final String description;

    OrderStatus(int code, String description) {
        this.code = code;
        this.description = description;
    }

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    // 通过 code 获取对应的枚举值
    public static OrderStatus fromCode(int code) {
        for (OrderStatus status : OrderStatus.values()) {
            if (status.getCode() == code) {
                return status;
            }
        }
        throw new IllegalArgumentException("Invalid order status code: " + code);
    }

    // 通过 description 获取对应的枚举值
    public static OrderStatus fromDescription(String description) {
        for (OrderStatus status : OrderStatus.values()) {
            if (status.getDescription().equals(description)) {
                return status;
            }
        }
        throw new IllegalArgumentException("Invalid order status description: " + description);
    }

     public static Set<Integer> getRevenueOrderStatus() {
        return CollUtil.newHashSet(
                 HAVE_BEEN_USED.getCode(),
                 HAVE_PAID.getCode()
         );
     }

    public static Set<Integer> getRefundOrderStatus() {
        return CollUtil.newHashSet(
                REFUNDED.getCode(),
                REFUND_LOADING.getCode(),
                REFUND_FAIL.getCode()
        );
    }

    @Override
    public String toString() {
        return "OrderStatus{" +
                "code=" + code +
                ", description='" + description + '\'' +
                '}';
    }
}
